package com.icecream.user.utils.req;


import com.icecream.user.utils.jwt.JwtHelper;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mr_h
 * @version 1.0
 * description: 获取request中需要的数据
 * create by Mr_h on 2018/6/22 0022
 */
@Slf4j
public class RequestHandler {

    public static Integer paramHandler(HttpServletRequest request) {
            return JwtHelper.parseJWT(request.getParameter("access_token")
                            .replace("customer", ""), "consumer").getUid();
    }

    public static Integer paramHandlerForStar(HttpServletRequest request) {
        return JwtHelper.parseJWT(request.getParameter("access_token")
                , "star").getUid();
    }

    public static Integer checkToken(Map<String, Object> map){
        return map.get("id") != null ? Integer.parseInt(map.get("id").toString()) : null;
    }


    /**
     * 去掉url中的路径，留下请求参数部分
     *
     * @param strURL url地址
     * @return url请求参数部分
     */
    private static String TruncateUrlPage(String strURL) {
        String strAllParam = null;
        String[] arrSplit = null;

        strURL = strURL.trim().toLowerCase();

        arrSplit = strURL.split("[?]");
        if (strURL.length() > 1) {
            if (arrSplit.length > 1) {
                if (arrSplit[1] != null) {
                    strAllParam = arrSplit[1];
                }
            }
        }

        return strAllParam;
    }

    public static Map<String, String> URLRequest(String URL) {
        Map<String, String> mapRequest = new HashMap<String, String>();

        String[] arrSplit = null;

        String strUrlParam = TruncateUrlPage(URL);
        if (strUrlParam == null) {
            return mapRequest;
        }
        //每个键值为一组 www.2cto.com
        arrSplit = strUrlParam.split("[&]");
        for (String strSplit : arrSplit) {
            String[] arrSplitEqual = null;
            arrSplitEqual = strSplit.split("[=]");

            //解析出键值
            if (arrSplitEqual.length > 1) {
                //正确解析
                mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);

            } else {
                if (arrSplitEqual[0] != "") {
                    //只有参数没有值，不加入
                    mapRequest.put(arrSplitEqual[0], "");
                }
            }
        }
        return mapRequest;
    }

}


